#include<iostream>
#include<TelChek.h>


#include<TROOT.h>
#include<TFile.h>
#include<TTree.h>
#include<TMath.h>
#include<TCanvas.h>
#include<TH1F.h>
#include<TH2F.h>
#include<TF1.h>
#include<TStyle.h>

#include<Global.h>

TelChek::TelChek(Int_t Value) {
	Private = Value;
	//RunNumber = Value;

}

void TelChek::Report(Int_t RunNumber) {

	gStyle->SetOptFit(1);
	gStyle->SetPalette(1);

	Global* glob;
	glob = new Global(1001);

	Float_t DigX[glob->TELNumberOfPlanes][glob->TELMaxHit];
	Float_t DigY[glob->TELNumberOfPlanes][glob->TELMaxHit];
	Float_t CogX[glob->TELNumberOfPlanes][glob->TELMaxHit];
	Float_t CogY[glob->TELNumberOfPlanes][glob->TELMaxHit];

	Char_t Path[200];
	sprintf(Path, "ntuple/tele/TELHits%u.root", RunNumber);
	TFile *trackfile;
	trackfile = new TFile(Path);
	trackfile->cd();
	TTree *Tracks = (TTree*) trackfile->Get("TELHits");

	Tracks->SetBranchAddress("DigX", &DigX[0][0]);
	Tracks->SetBranchAddress("DigY", &DigY[0][0]);
	Tracks->SetBranchAddress("CogX", &CogX[0][0]);
	Tracks->SetBranchAddress("CogY", &CogY[0][0]);

	//Residuals Dig
	TH1F* h11[2][glob->TELNumberOfPlanes];
	TCanvas* canv11 = new TCanvas("c1", "", 1200, 800);
	canv11->SetFillColor(0);
	canv11->Divide(glob->TELNumberOfPlanes, 2);

	//Residuals Cog
	TH1F* h12[2][glob->TELNumberOfPlanes];
	TCanvas* canv12 = new TCanvas("c2", "", 1200, 800);
	canv12->SetFillColor(0);
	canv12->Divide(glob->TELNumberOfPlanes, 2);

	//Residuals Cog
	TH2F* h21[2][glob->TELNumberOfPlanes];
	TCanvas* canv21 = new TCanvas("c3", "", 1200, 800);
	canv21->SetFillColor(0);
	canv21->Divide(glob->TELNumberOfPlanes, 2);

	Char_t TELRoot[100];

	for (Int_t n = 0; n < glob->TELNumberOfPlanes; n++) {

		sprintf(TELRoot, "ResDig_X_%u", n + 1);
		h11[0][n] = new TH1F(TELRoot, "", 100, -glob->TELCorrIntervalX[0], glob->TELCorrIntervalX[0]);
		h11[0][n]->GetYaxis()->SetTitle("Number of events");
		h11[0][n]->GetXaxis()->SetTitle("Res_{X} [#mum]");

		sprintf(TELRoot, "ResDig_Y_%u", n + 1);
		h11[1][n] = new TH1F(TELRoot, "", 100, -glob->TELCorrIntervalY[0], glob->TELCorrIntervalY[0]);
		h11[1][n]->GetYaxis()->SetTitle("Number of events");
		h11[1][n]->GetXaxis()->SetTitle("Res_{Y} [#mum]");

		sprintf(TELRoot, "ResCog_X_%u", n + 1);
		h12[0][n] = new TH1F(TELRoot, "", 100, -glob->TELCorrIntervalX[0], glob->TELCorrIntervalX[0]);
		h12[0][n]->GetYaxis()->SetTitle("Number of events");
		h12[0][n]->GetXaxis()->SetTitle("Res_{X} [#mum]");

		sprintf(TELRoot, "ResCog_Y_%u", n + 1);
		h12[1][n] = new TH1F(TELRoot, "", 100, -glob->TELCorrIntervalY[0], glob->TELCorrIntervalY[0]);
		h12[1][n]->GetYaxis()->SetTitle("Number of events");
		h12[1][n]->GetXaxis()->SetTitle("Res_{Y} [#mum]");

		sprintf(TELRoot, "Res_X_ver_Y_%u", n + 1);
		h21[0][n] = new TH2F(TELRoot, "", glob->TELNumberOfStripsY[n], 0, glob->TELNumberOfStripsY[n] * glob->TELStripSizeY[n], 100, -glob->TELCorrIntervalX[0], glob->TELCorrIntervalX[0]);
		h21[0][n]->GetYaxis()->SetTitle("Res_{X} [#mum]");
		h21[0][n]->GetXaxis()->SetTitle("Y [#mum]");

		sprintf(TELRoot, "Res_Y_ver_X_%u", n + 1);
		h21[1][n] = new TH2F(TELRoot, "", glob->TELNumberOfStripsX[n], 0, glob->TELNumberOfStripsX[n] * glob->TELStripSizeX[n], 100, -glob->TELCorrIntervalY[0], glob->TELCorrIntervalY[0]);
		h21[1][n]->GetYaxis()->SetTitle("Res_{Y} [#mum]");
		h21[1][n]->GetXaxis()->SetTitle("X [#mum]");

	}

	Int_t while1;
	Int_t while2;

	//o// read events
	for (Int_t i = 0; i < Tracks->GetEntries(); i++) {
		Int_t k=Tracks->GetEntries();
		printf("Entries=%d", k);
		// loop over entries
		Tracks->GetEntry(i);
		while2 = 0;
		for (Int_t n = 0; n < glob->TELNumberOfPlanes - 1; n++) {
			for (Int_t m = n + 1; m < glob->TELNumberOfPlanes; m++) {
				while1 = 0;
				while (DigX[n][while1] > 0 && DigX[m][while1] > 0 && while1 < glob->TELMaxHit) {
					h11[0][while2]->Fill(DigX[n][while1] - DigX[m][while1]);
					sprintf(TELRoot, "Dig_X_%u_%u", n + 1, m + 1);
					h11[0][while2]->SetTitle(TELRoot);
					//h21[0][while2]->Fill(DigY[n][while1],DigX[n][while1] - DigX[m][while1]);
					//sprintf(TELRoot,"TEL_%u_%u",n+1,m+1);
					//h21[0][while2]->SetTitle(TELRoot);
					while1++;
				}
				while1 = 0;
				while (DigY[n][while1] > 0 && DigY[m][while1] > 0 && while1 < glob->TELMaxHit) {
					h11[1][while2]->Fill(DigY[n][while1] - DigY[m][while1]);
					sprintf(TELRoot, "Dig_Y_%u_%u", n + 1, m + 1);
					h11[1][while2]->SetTitle(TELRoot);
					//h21[1][while2]->Fill(DigX[n][while1],DigY[n][while1] - DigY[m][while1]);
					//sprintf(TELRoot,"TEL_%u_%u",n+1,m+1);
					//h21[1][while2]->SetTitle(TELRoot);
					while1++;
				}
				while1 = 0;
				while (CogX[n][while1] > 0 && CogX[m][while1] > 0 && CogY[n][while1] > 0 && while1 < glob->TELMaxHit) {
					h12[0][while2]->Fill(CogX[n][while1] - CogX[m][while1]);
					sprintf(TELRoot, "Cog_X_%u_%u", n + 1, m + 1);
					h12[0][while2]->SetTitle(TELRoot);
					h21[0][while2]->Fill(CogY[n][while1], CogX[n][while1] - CogX[m][while1]);
					sprintf(TELRoot, "TEL_%u_%u", n + 1, m + 1);
					h21[0][while2]->SetTitle(TELRoot);
					while1++;
				}
				while1 = 0;
				while (CogY[n][while1] > 0 && CogY[m][while1] > 0 && CogX[n][while1] > 0 && while1 < glob->TELMaxHit) {
					h12[1][while2]->Fill( CogY[n][while1] - CogY[m][while1] );
					sprintf(TELRoot, "Cog_Y_%u_%u", n + 1, m + 1);
					h12[1][while2]->SetTitle(TELRoot);
					h21[1][while2]->Fill( CogX[n][while1], CogY[n][while1] - CogY[m][while1] );
					sprintf(TELRoot, "TEL_%u_%u", n + 1, m + 1);
					h21[1][while2]->SetTitle(TELRoot);
					while1++;
				}
				while2++;
			}//m
		}//n
	}//i

	for (Int_t n = 0; n < glob->TELNumberOfPlanes; n++) {

		canv11->cd(n + 1);
		h11[0][n]->Draw();
		h11[0][n]->Fit("gaus");
		canv11->cd(n + 1 + glob->TELNumberOfPlanes);
		h11[1][n]->Draw();
		h11[1][n]->Fit("gaus");

		canv12->cd(n + 1);
		h12[0][n]->Draw();
		h12[0][n]->Fit("gaus");
		canv12->cd(n + 1 + glob->TELNumberOfPlanes);
		h12[1][n]->Draw();
		h12[1][n]->Fit("gaus");

		canv21->cd(n + 1);
		canv21->GetPad(n + 1)->SetGridx();
		canv21->GetPad(n + 1)->SetGridy();
		h21[0][n]->Draw("COLZ");
		canv21->cd(n + 1 + glob->TELNumberOfPlanes);
		canv21->GetPad(n + 1 + glob->TELNumberOfPlanes)->SetGridx();
		canv21->GetPad(n + 1 + glob->TELNumberOfPlanes)->SetGridy();
		h21[1][n]->Draw("COLZ");

	}//n
}
TelChek::~TelChek() {

}
